package middleware

import (
	"earth/models"
	"regexp"

	"github.com/gin-gonic/gin"
)

func Auth() gin.HandlerFunc {
	return func(c *gin.Context) {
		authHeader := c.Request.Header.Get("Authorization")
		r, _ := regexp.Compile("^Bearer (.+)$")

		match := r.FindStringSubmatch(authHeader)

		if len(match) == 0 {
			c.AbortWithStatus(401)
			return
		}
		tokenString := match[1]

		if len(tokenString) == 0 {
			c.AbortWithStatus(401)
			return
		}

		user, err := models.GetUserByAuthToken(tokenString)
		if err != nil {
			c.AbortWithStatus(401)
			return
		}

		c.Set("user", user)
		c.Set("userID", user.ID)
		c.Next()
	}
}
